import logging
import time

from commons_util import Util
import pyautogui
import pyperclip
from datetime import datetime


class Account:
    def __init__(self, username, passcode, characters, comment, account_type, same_group_account):
        self.username = username
        self.passcode = passcode
        self.characters = characters
        self.comment = comment
        self.account_type = account_type
        self.same_group_account = same_group_account
        self.character_index = 0
        pass

    def has_next_character(self):
        return self.character_index < len(self.characters)

    def next_character(self):
        ch_dic = self.characters[self.character_index]
        ch = Account.Character(index=self.character_index,
                               nickname=ch_dic['nickname'],
                               occupation=ch_dic['occupation'],
                               )
        logging.info(f'加载角色：{ch.index}, {ch.nickname} : {ch.occupation}')
        self.character_index += 1
        return ch

    def switch_account(self):
        # 退出已有账号
        Util.click(975, 908, delay=1)
        Util.click(932, 1160, delay=2)
        Util.click(650, 1381, delay=2)
        # Util.click(907, 1397, delay=2)

        if self.account_type == 4:
            Util.click(763, 1561, 1)
        elif self.account_type == 5:
            Util.click(837, 1560, 1)

        Util.click(858, 1142, 1)
        Util.click(650, 1140, 1)
        # Util.click(588, 1088, 1)
        pyperclip.copy(self.username)
        pyautogui.hotkey('ctrl', 'v')
        time.sleep(0.2)
        Util.click(647, 1207)
        pyperclip.copy(self.passcode)
        pyautogui.hotkey('ctrl', 'v')
        time.sleep(0.2)
        Util.click(652, 1434, delay=3)
        # print('enter verify code')
        # a = input()

    class Character:
        def __init__(self, index, nickname, occupation):
            self.index = index
            self.nickname = nickname
            self.occupation = occupation
            self.is_first = True

        def xiayi_once(self, num_judian=3, num_xianshi=2):
            self.reset(0)

            self.judian(num_judian)
            time.sleep(1)

            self.xianshi(num_xianshi)
            time.sleep(1)

        # 精英据点
        def judian(self, num):
            print("开始据点：")
            for i in range(num):
                print(f"据点第{i + 1}次")
                time_delay = 5
                if i == 0:
                    time_delay = 25
                pyautogui.press('f1')
                time.sleep(0.5)
                Util.click(659, 1434, 1)
                Util.click(257, 1067, 0.5)
                Util.click(997, 1426, 0.5)
                time.sleep(time_delay)
                Util.click(797, 1190, 1)
                Util.click(591, 1518, 1)

                if i == 0:
                    time.sleep(40 + 30)
                else:
                    time.sleep(40)

                # 结算
                Util.click(618, 971, 1)
                Util.click(618, 971, 1)

        # 仙师试炼
        def xianshi(self, count):
            print("开始仙师")
            for i in range(count):
                print(f"仙师第{i + 1}次")
                pyautogui.press('f1')
                time.sleep(0.5)
                Util.click(659, 1434, 1)
                Util.click(213, 1227, 1)
                Util.click(912, 1419, 1)
                Util.click(879, 1254, 1)
                time.sleep(20)
                Util.click(779, 1255, 1)
                Util.click(757, 1251, 1)
                time.sleep(160)
                # Util.click(791, 1007, 3)
                pass

        # 镇魔令
        def zhenmo_once(self):
            print('镇魔开始')
            self.reset(0)
            pyautogui.press('f1')
            time.sleep(2)
            Util.click(304, 1204, 1)
            time.sleep(25 * 60)
            self.reset(1)
            self.reset(1)
            self.reset(0)
            self.reset(0)
            Util.click(795, 1317, 1)
            print('镇魔结束')
            pass

        def get_leader(self):
            self.reset(0)
            pyautogui.press('T')
            time.sleep(1)
            Util.click(1033, 1083, 1)
            Util.click_s(198, 1165)
            Util.click(638, 1322, 1)
            Util.click(706, 2073, 1)
            self.reset(0)
            pyautogui.press('T')
            time.sleep(1)
            pyautogui.press('esc')

        def reset(self, window=0):
            if window == 0:
                Util.click(613, 994, 2)
            elif window == 1:
                Util.click(628, 1646, 2)

        def put_leader(self):
            self.reset(1)

            # pyautogui.press('enter')
            # time.sleep(0.5)
            # pyautogui.press('esc')
            # time.sleep(0.5)
            pyautogui.press('T')
            time.sleep(0.5)
            Util.click(1030, 1777, 1)
            Util.click_s(207, 1853, 1)
            Util.click(637, 2009, 1)
            pyautogui.press('T')
            time.sleep(1)
            Util.click(711, 1384, 1)

            # 退出队伍
            self.reset(0)
            pyautogui.press('T')
            time.sleep(1)
            Util.click(717, 1499, 1)
            Util.click(704, 1385, 1)
            pyautogui.press('T')

        def enter_group(self):
            self.reset(0)
            pyautogui.press('R')
            time.sleep(0.5)
            Util.click(61, 1116, 1)
            Util.click_s(535, 1022, 0.2)
            time.sleep(1)
            Util.click(640, 1322, 1)

            # 同意入队
            self.reset(1)
            pyautogui.press('enter')
            time.sleep(1)
            pyautogui.press('esc')
            time.sleep(1)
            pyautogui.press('T')
            time.sleep(1)
            Util.click(1030, 1776, 1)
            Util.click(377, 2181, 1)
            Util.click(725, 1765, 1)
            Util.click(900, 1705, 1)
            pyautogui.press('T')
            time.sleep(1)
            # pyautogui.press('enter')
            # time.sleep(1)
            # Util.click(58, 1853)

        def test(self):
            for i in range(len(self.accounts)):
                for j in range(self.accounts[i]['num']):
                    if i == 0:
                        continue
                    ty = 0
                    if self.accounts[i]['num'] > 1 and j > 0:
                        ty = 1
                    if self.is_first:
                        ty = 1
                        self.is_first = False
                    self.switch_account(self.accounts[i], j, ty)

        def switch_character(self):

            self.reset(0)
            pyautogui.press('esc')
            time.sleep(1)
            Util.click(177, 1495, 3)

            Util.click(930, 1412, 1)
            if self.index == 0:
                Util.click(181, 1067, 1)
                pass
            elif self.index == 1:
                Util.click(181, 1166, 1)
                pass
            elif self.index == 2:
                Util.click(181, 1264, 1)
                pass
            elif self.index == 3:
                Util.click(181, 1353, 1)
                pass

            ## 点击登录
            Util.click(1037, 1517, 1)


class AccountGenerator:

    def __init__(self):
        self.current_account_index = 0
        pass

    def has_next(self):
        return self.current_account_index < len(AccountGenerator.accounts)

    def next(self):
        cur_acc_dic = AccountGenerator.accounts[self.current_account_index]
        self.current_account_index += 1
        acc = Account(
            username=cur_acc_dic['user'],
            passcode=cur_acc_dic['passcode'],
            characters=cur_acc_dic['characters'],
            comment=cur_acc_dic['comment'],
            account_type=cur_acc_dic['type'],
            same_group_account=cur_acc_dic['used_account'],
        )
        logging.info(f'加载账号{self.current_account_index}：{acc.username}, {acc.comment}')
        logging.info(f'剩余账号{len(self.accounts) - self.current_account_index}')
        return acc
